//
//  UIColor+LSColor.m
//  GenericProject
//
//  Created by 社科赛斯 on 2019/12/12.
//  Copyright © 2019 漠然丶情到深处. All rights reserved.
//

#import "UIColor+LSColor.h"

@implementation UIColor (LSColor)

+ (UIColor *)ls_colorWithHexString:(NSString *)color Alpha:(CGFloat)alpha {
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    //判断前缀
    if ([cString hasPrefix:@"0X"]) {
        cString = [cString substringFromIndex:2];
    }
    if ([cString hasPrefix:@"#"]) {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6) {
        return [UIColor clearColor];
    }
    
    //从六位数值中找到RGB对应的位数并转换
    NSRange range;
    range.location = 0;
    range.length = 2;
    //R G B
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

+ (UIColor *)ls_colorWithHexString:(NSString *)color {
    return [self ls_colorWithHexString:color Alpha:1.0f];
}

+ (NSString *)ls_hexFromUIColor:(UIColor *)color {
    if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        color = [UIColor colorWithRed:components[30] green:components[141] blue:components[13] alpha:components[1]];
    }
    if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
        return [NSString stringWithFormat:@"#FFFFFF"];
    }
    return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(color.CGColor))[0] * 255.0), (int)((CGColorGetComponents(color.CGColor))[1] * 255.0), (int)((CGColorGetComponents(color.CGColor))[2] * 255.0)];
}

//滑动改变渐变色
+ (NSArray *)ls_getRGBWithFromColor:(NSString *)fromColor toColor:(NSString *)toColor shade:(CGFloat)mShade {
    NSArray *fromeRgb = [self ls_getRGBWithColor:fromColor];
    NSArray *toRgb = [self ls_getRGBWithColor:toColor];
    
    int fromR = [fromeRgb[0]intValue];
    int fromG = [fromeRgb[1]intValue];
    int fromB = [fromeRgb[2]intValue];
    
    int toR = [toRgb[0]intValue];
    int toG = [toRgb[1]intValue];
    int toB = [toRgb[2]intValue];
    
    int diffR = toR - fromR;
    int diffG = toG - fromG;
    int diffB = toB - fromB;
    
    int red = fromR + (int) ((diffR * mShade));
    int green = fromG + (int) ((diffG * mShade));
    int blue = fromB + (int) ((diffB * mShade));
    
    return @[@(red /255.0f),@(green /255.0f),@(blue /255.0f)];
}

+ (NSArray *)ls_getRGBWithColor:(NSString *)color {
    // 从六位数值中找到RGB对应的位数并转换
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //R、G、B
    NSString *rString = [color substringWithRange:range];
    range.location = 2;
    
    NSString *gString = [color substringWithRange:range];
    range.location = 4;
    
    NSString *bString = [color substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return @[@(r),@(g),@(b)];
}

@end
